package com.ruoyi.project.module.gs;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/gsdetails")
public class GsYqgConsumptionDetailsController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    /**
     * 查询消费详细列表
     */
    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }

    /**
     * 分单
     */
    @Log(title="分单",businessType = BusinessType.SPLITORDER)
       @PostMapping(value = "/fendan/{leftid}/{rightid}")
    public AjaxResult getInfo(@PathVariable("leftid") String leftid,@PathVariable("rightid") String rightid,@RequestBody Map<String,Object> map) {
          List<Integer> idsList = (List<Integer>) map.get("ids");
           Integer[] ids = idsList.toArray(new Integer[0]);
           List<Integer> noteList = (List<Integer>) map.get("note5");
           System.out.println(noteList.get(0)+"note11");


//77
          for(int i=0;i<ids.length;i++){

               YqgBarBills yqgBarBills=new YqgBarBills();
               yqgBarBills.setHandBrand(rightid);
               yqgBarBills.setHandState("1");
               List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);




               YqgConsumptionDetails ppp=   yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(Long.valueOf(ids[i]));



              //如果分到数量是1，将ppp这条数据整体赋值给rightid的YqgConsumptionDetails表，
              // 并且将quantity设置为note5，将ppp原来的quantity设置为quantity-note5

              // 创建一个新的 YqgConsumptionDetails 对象，并将其属性设置为 ppp 的属性
              YqgConsumptionDetails newPpp = new YqgConsumptionDetails();
              newPpp.setUserId(ppp.getUserId());
              newPpp.setDeptId(ppp.getDeptId());
              newPpp.setDepartment(ppp.getDepartment());
              newPpp.setNickName(ppp.getNickName());
              newPpp.setUserName(ppp.getUserName());
              newPpp.setPost(ppp.getPost());

              newPpp.setOriginalHand(ppp.getOriginalHand());
              newPpp.setHandCategory(ppp.getHandCategory());
              newPpp.setHandState(ppp.getHandState());
              newPpp.setRetentionStatus(ppp.getRetentionStatus());
              newPpp.setMergeStatus(ppp.getMergeStatus());
              newPpp.setOpeningTime(ppp.getOpeningTime());
              newPpp.setDrawTime(ppp.getDrawTime());
              newPpp.setMergeHands(ppp.getMergeHands());
              newPpp.setMergeBarBillsId(ppp.getMergeBarBillsId());
              newPpp.setLeaveTime(ppp.getLeaveTime());
              newPpp.setBeforeLeave(ppp.getBeforeLeave());
              newPpp.setBeforeBarBillsId(ppp.getBeforeBarBillsId());
              newPpp.setEntryName(ppp.getEntryName());
              newPpp.setProjectCategory(ppp.getProjectCategory());
              newPpp.setProjectCode(ppp.getProjectCode());
              newPpp.setMnemonicCode(ppp.getMnemonicCode());
              newPpp.setUnit(ppp.getUnit());
              newPpp.setUnitPrice(ppp.getUnitPrice());
              newPpp.setCommissionPrice(ppp.getCommissionPrice());
              newPpp.setTogether(ppp.getTogether());
              newPpp.setHandoverStatus(ppp.getHandoverStatus());
              newPpp.setSailingsSigns(ppp.getSailingsSigns());
              newPpp.setBarBillsId(ppp.getBarBillsId());
              newPpp.setRecordingTime(ppp.getRecordingTime());

              //分单数量
             newPpp.setQuantity(String.valueOf(noteList.get(i)));

             int p1=Integer.valueOf(ppp.getUnitPrice());
              System.out.println(noteList.get(i)+"pp");
            int p2= Integer.valueOf(String.valueOf(noteList.get(i)));
             int p3=p1*p2;

              newPpp.setTotalAmount(p3+"");
              newPpp.setSeat(ppp.getSeat());
              newPpp.setOrderNumber(ppp.getOrderNumber());
              newPpp.setPaymentMethod(ppp.getPaymentMethod());
              newPpp.setConsumptionStatus(ppp.getConsumptionStatus());
              newPpp.setChargebackTime(ppp.getChargebackTime());
              newPpp.setReasonForChargeback(ppp.getReasonForChargeback());
              newPpp.setChargebackProcessorAccount(ppp.getChargebackProcessorAccount());
              newPpp.setChargebackProcessorName(ppp.getChargebackProcessorName());
              newPpp.setClockTime(ppp.getClockTime());
              newPpp.setNextClockTime(ppp.getNextClockTime());
              newPpp.setNextClockStatus(ppp.getNextClockStatus());
              newPpp.setRecordEmployeeNumber(ppp.getRecordEmployeeNumber());
              newPpp.setRecorderEmployeeName(ppp.getRecorderEmployeeName());
              newPpp.setRoomType(ppp.getRoomType());
              newPpp.setOpeningHours(ppp.getOpeningHours());
              newPpp.setCheckOutTime(ppp.getCheckOutTime());
              newPpp.setCheckOutStatus(ppp.getCheckOutStatus());





              // 将新对象赋值给 rightid 的 YqgConsumptionDetails 表
              newPpp.setHandBrand(rightid);
              newPpp.setBarBillsId(list.get(0).getID());
              newPpp.setNote10("来自"+leftid+"的分单");
              yqgConsumptionDetailsService.insertYqgConsumptionDetails(newPpp);

              // 将原始 ppp 的 quantity 改为 quantity - note5，再更新至原表
              int newQuantity = Integer.parseInt(ppp.getQuantity()) - Integer.parseInt(String.valueOf(noteList.get(i)));

              if(newQuantity>0){
                  ppp.setQuantity(String.valueOf(newQuantity));
                  ppp.setTotalAmount(String.valueOf(newQuantity*Integer.valueOf(ppp.getUnitPrice())));
                  yqgConsumptionDetailsService.updateYqgConsumptionDetails(ppp);
              }else {
                  yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByID(Long.valueOf(ids[i]));
              }












        }
        return success();
    }



}
